﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace 校园网小工具
{
    public partial class LoginForm : Form
    {
        public LoginForm()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (!backgroundWorker1.IsBusy)
            {
                backgroundWorker1.RunWorkerAsync();
            }
        }
        public static bool isTrue = false;
        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            backgroundWorker1.ReportProgress(0, "登录中");
            using (var dc=new UserInfoDataContext())
            {
                var isRight = dc.UserInfo.FirstOrDefault(o=>o.Login==textBox1.Text &&o.Password==textBox2.Text);
                if (isRight != null && isRight.isReady.ToString().Replace(" ",null)=="true")
                {
                    isTrue = true;                   
                }
               
            }
            backgroundWorker1.ReportProgress(0, "登录完成");
        }

        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (isTrue == true)
            {
                Form1.isFree = true;
                Form1.shouldConnect = false;
                Form1 f = new Form1();
                this.Hide();
                f.Show();
            }
            else
            {
                MessageBox.Show("用户名密码输入错误！");
                label3.Text = "请登陆后再使用";
            }
        }

        private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            label3.Text = e.UserState.ToString();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void LoginForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            Application.Exit();
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            string target = "http://item.taobao.com/item.htm?spm=686.1000925.1000774.13.RaNhTW&id=37608265896";
            try
            {
                System.Diagnostics.Process.Start(target);
            }
            catch (System.ComponentModel.Win32Exception noBrowser)
            {
                if (noBrowser.ErrorCode == -2147467259)
                    MessageBox.Show(noBrowser.Message);
            }
            catch (System.Exception other)
            {
                MessageBox.Show(other.Message);
            } 
        }
    }
}
